package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.config.mall.ConfigExpressVO;
import com.cskaoyan.bean.config.mall.ConfigMallVO;
import com.cskaoyan.bean.config.mall.ConfigOrderVO;
import com.cskaoyan.bean.config.mall.ConfigWxVO;
import com.cskaoyan.bean.po.MarketSystem;
import com.cskaoyan.bean.po.MarketSystemExample;
import com.cskaoyan.mapper.ConfigMapper;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author chenai
 * @description TODO
 * @date 2022-06-07 17:09
 */
//@EnableTransactionManagement
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Autowired
    ConfigMapper configMapper;

    @Override
    public BaseRespVo mall() {
        return BaseRespVo.ok(new ConfigMallVO(configMapper.selectByKeyName("market_mall_address"),
                configMapper.selectByKeyName("market_mall_latitude"),
                configMapper.selectByKeyName("market_mall_longitude"),
                configMapper.selectByKeyName("market_mall_name"),
                configMapper.selectByKeyName("market_mall_phone"),
                configMapper.selectByKeyName("market_mall_qq")));
    }

    @Transactional
    @Override
    public BaseRespVo mallUpdate(ConfigMallVO configMallVO) {
        try {
            configMapper.updateValueByKeyName("market_mall_address", configMallVO.getMarket_mall_address());
            configMapper.updateValueByKeyName("market_mall_latitude", configMallVO.getMarket_mall_latitude());
            configMapper.updateValueByKeyName("market_mall_longitude", configMallVO.getMarket_mall_longitude());
            configMapper.updateValueByKeyName("market_mall_name", configMallVO.getMarket_mall_name());
            configMapper.updateValueByKeyName("market_mall_phone", configMallVO.getMarket_mall_phone());
            configMapper.updateValueByKeyName("market_mall_qq", configMallVO.getMarket_mall_qq());
            return BaseRespVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("修改失败");
        }
    }

    @Override
    public BaseRespVo express() {
        return BaseRespVo.ok(new ConfigExpressVO(configMapper.selectByKeyName("market_express_freight_min"),
                configMapper.selectByKeyName("market_express_freight_value")));
    }

    @Transactional
    @Override
    public BaseRespVo expressUpdate(ConfigExpressVO configExpressVO) {
        try {
            configMapper.updateValueByKeyName("market_express_freight_min", configExpressVO.getMarket_express_freight_min());
            configMapper.updateValueByKeyName("market_express_freight_value", configExpressVO.getMarket_express_freight_value());
            return BaseRespVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("修改失败");
        }
    }
//    market_order_comment: "19"
//    market_order_unconfirm: "6"
//    market_order_unpaid: "30"
    @Override
    public BaseRespVo order() {
        return BaseRespVo.ok(new ConfigOrderVO(configMapper.selectByKeyName("market_order_comment"),
                configMapper.selectByKeyName("market_order_unconfirm"),
                configMapper.selectByKeyName("market_order_unpaid")));
    }
    @Transactional
    @Override
    public BaseRespVo orderUpdate(ConfigOrderVO configOrderVO) {
        try {
            configMapper.updateValueByKeyName("market_order_comment", configOrderVO.getMarket_order_comment());
            configMapper.updateValueByKeyName("market_order_unconfirm", configOrderVO.getMarket_order_unconfirm());
            configMapper.updateValueByKeyName("market_order_unpaid", configOrderVO.getMarket_order_unpaid());
            return BaseRespVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("修改失败");
        }
    }

//    market_wx_catlog_goods: "9"
//    market_wx_catlog_list: "2"
//    market_wx_index_brand: "4"
//    market_wx_index_hot: "7"
//    market_wx_index_new: "5"
//    market_wx_index_topic: "3"
//    market_wx_share: "true"
    @Override
    public BaseRespVo wx() {
        return BaseRespVo.ok(new ConfigWxVO(configMapper.selectByKeyName("market_wx_catlog_goods"),
                configMapper.selectByKeyName("market_wx_catlog_list"),
                configMapper.selectByKeyName("market_wx_index_brand"),
                configMapper.selectByKeyName("market_wx_index_hot"),
                configMapper.selectByKeyName("market_wx_index_new"),
                configMapper.selectByKeyName("market_wx_index_topic"),
                configMapper.selectByKeyName("market_wx_share")));
    }

    @Override
    public BaseRespVo wxUpdate(ConfigWxVO configWxVO) {
        try {
            configMapper.updateValueByKeyName("market_wx_catlog_goods", configWxVO.getMarket_wx_catlog_goods());
            configMapper.updateValueByKeyName("market_wx_catlog_list", configWxVO.getMarket_wx_catlog_list());
            configMapper.updateValueByKeyName("market_wx_index_brand", configWxVO.getMarket_wx_index_brand());
            configMapper.updateValueByKeyName("market_wx_index_hot", configWxVO.getMarket_wx_index_hot());
            configMapper.updateValueByKeyName("market_wx_index_new", configWxVO.getMarket_wx_index_new());
            configMapper.updateValueByKeyName("market_wx_index_topic", configWxVO.getMarket_wx_index_topic());
            configMapper.updateValueByKeyName("market_wx_share", configWxVO.getMarket_wx_share());
            return BaseRespVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("修改失败");
        }
    }
}
